window.baiaimama = {};

Zepto(function ($) {
    /**
     * 基础hover事件模拟器
     * @param e
     */
    //FastClick.attach(document.body);

    if ($("img.lazy").length > 0) {
        $("img.lazy").lazyload({
            effect: "fadeIn"
        });
    }

    document.documentElement.ontouchstart = function (e) {
        if ($(e.target).attr('data-onhover') && $(e.target).data('onhover')) {
            $(e.target).addClass($(e.target).data('onhover'));
        }
    }
    document.documentElement.ontouchend = function () {
        $('[data-onhover]').each(function () {
            $(this).removeClass($(this).data('onhover'));
        });
    }
    $("body").delegate('[data-href]', 'click', function (e) {//页面跳转
        window.location.href = $(this).data('href');
        return false;
    }).delegate('[data-clicktips]', 'click', function () {//显示提示文字
        baiaimama.Toast.New($(this).data('clicktips'));
    }).delegate('[data-spopup]', 'click', function () {//显示弹出框
        if ($($(this).data('spopup'))) {
            $($(this).data('spopup')).show();
            $($(this).data('spopup')).siblings().addClass("disableClick");
        };
    }).delegate('[data-cpopup]', 'click', function () {//隐藏弹出框
        if ($($(this).data('cpopup'))) {
            $($(this).data('cpopup')).hide();
            $($(this).data('cpopup')).siblings().removeClass("disableClick");
        };
    });
    if (document.getElementById('pullWrapper')) {
        document.getElementById('pullWrapper').addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
    }

});

baiaimama.Toast = {
    isopen: true,
    ins: null,
    intval: 0,
    New: function (msg, remain) {
        var that = this;
        if (this.isopen && this.ins) {
            this.clear();
        }

        this.instance(msg);
        remain || (remain = 3);
        this.intval = setTimeout(function () {
            that.ins.css('opacity', 0);
        }, remain * 1000);
    },
    instance: function (msg) {
        this.ins = $('<div id="global-toast"></div>');
        this.ins.appendTo('body');
        this.ins.html(msg);
        this.ins.css('margin-left', (-this.ins.width() / 2) + 'px');
        this.ins.show();
        var that = this;
        this.ins[0].addEventListener("transitionend", function () {
            that.clear();
        });
        this.ins[0].addEventListener("webkitTransitionEnd", function () {
            that.clear();
        });
    },
    clear: function () {
        this.ins.remove();
        this.ins = null;
        clearTimeout(this.intval);
    }
};

//显示优酷视频
function showVideo(domid, vid) {
    player = new YKU.Player(domid, {
        styleid: '0',
        client_id: 'b42e9396dd100820',
        vid: vid,
        show_related: false
    });
    initVideoSize($("#" + domid));
    $(window).resize(function () {
        initVideoSize($("#" + domid));
    });
    function initVideoSize(obj) {
        obj.height(obj.width() * 9 / 16);
    }
}


//列表内容绑定dropload
function setDropload(wrapper, buildFunction) {
    if (!wrapper || !(wrapper.find(".c-list-page")) || !buildFunction) return false;
    var obj = wrapper.find(".c-list-page");
    baiaimama.dropload = wrapper.dropload({
        domUp: {
            domClass: 'dropload-up',
            domRefresh: '<div class="dropload-refresh">↓下拉刷新</div>',
            domUpdate: '<div class="dropload-update">↑释放更新</div>',
            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
        },
        domDown: {
            domClass: 'dropload-down',
            domRefresh: '<div class="dropload-refresh">↑上拉加载更多</div>',
            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
            domNoData: '<div class="dropload-noData">暂无数据</div>'
        },
        loadUpFn: function (me) {
            var apiUrl = $("#c-list-url").val() ? $("#c-list-url").val() : "",
            apiPraram = $("#c-list-param").val() ? $("#c-list-param").val() : "",
            apiIndex = 0;
            if (apiUrl == "") return false;
            $.ajax({
                type: 'get',
                url: apiUrl,
                data: (apiPraram == "") ? ("index=" + apiIndex) : (apiPraram + "&index=" + apiIndex),
                contentType: "application/json",
                success: function (data) {
                    var res = $.parseJSON(data);
                    if (res.result == false) {
                        baiaimama.Toast.New(res.msg);
                        return false;
                    }
                    if (res.result == true && res.data == null) {
                        obj.children("ul").hide();
                        obj.children(".no-result").show();
                        me.lock('down');
                        $(".dropload-down").hide();
                        me.resetload();
                        return false;
                    }
                    if (!(res.result == false) && !(res.data == null)) {
                        obj.children(".no-result").hide();
                        obj.children("ul").html(buildFunction(res)).show();
                        resetSize($(".c-list-left"));
                        $('img.lazy').lazyload();
                        $("#c-list-index").val(1);
                        if (res.data.lists.length < 10) {
                            $(".dropload-down").hide();
                            me.lock('down');
                        }
                        me.resetload();
                    }
                },
                error: function (xhr, type) {
                    baiaimama.Toast.New('网络异常！');
                    me.resetload();
                }
            });
        },
        loadDownFn: function (me) {
            var apiUrl = $("#c-list-url").val() ? $("#c-list-url").val() : "",
            apiPraram = $("#c-list-param").val() ? $("#c-list-param").val() : "",
            apiIndex = $("#c-list-index").val() ? $("#c-list-index").val() : 0;
            if (apiUrl == "") return false;
            $.ajax({
                type: 'get',
                url: apiUrl,
                data: (apiPraram == "") ? ("index=" + apiIndex) : (apiPraram + "&index=" + apiIndex),
                contentType: "application/json",
                success: function (data) {
                    var res = $.parseJSON(data);
                    if (!(res.result == false) && !(res.data == null)) {
                        apiIndex++;
                        $("#c-list-index").val(apiIndex);
                        obj.children("ul").append(buildFunction(res));
                        resetSize($(".c-list-left"));
                        $('img.lazy').lazyload();
                        if (res.data.lists.length < 10) {
                            $(".dropload-down").hide();
                            me.lock('down');
                        }
                    }
                    else {
                        $(".dropload-down").hide();
                        me.lock('down');
                    }
                    me.resetload();
                },
                error: function (xhr, type) {
                    baiaimama.Toast.New('网络异常！');
                    me.resetload();
                }
            });
        }
    });
    $(window).resize(function () {
        baiaimama.dropload.resetload();
    });
}


//验证
function validateInput(obj) {
    if ($.trim($("#" + obj ).val()) == "" && baiaimama.validated) {
        baiaimama.Toast.New($("#" + obj ).data("message"));
        baiaimama.validated = false;
    }
}
function validateTextarea(obj) {
    if ($.trim($("#" + obj ).val()) == "" && baiaimama.validated) {
        baiaimama.Toast.New($("#" + obj ).data("message"));
        baiaimama.validated = false;
    }
}
function validatePhone(obj) {
    var phone = $.trim($("#" + obj ).eq(0).val());
    if (!/^1[1-9]\d{9}$/.test(phone) && baiaimama.validated) {
        baiaimama.Toast.New('手机格式不正确');
        baiaimama.validated = false;
    }
}
function validateSelect(obj) {
    if ($.trim($("#" + obj ).find("option").not(function () { return !this.selected }).val()) == "" && baiaimama.validated) {
        baiaimama.Toast.New($("#" + obj ).data("message"));
        baiaimama.validated = false;
    }
}
function validateCardnumber(obj) {
    var phone = $.trim($("#" + obj ).eq(0).val());
    if (!/^[0-9]*$/.test(phone) && baiaimama.validated) {
        baiaimama.Toast.New('卡号格式不正确');
        baiaimama.validated = false;
    }
}

//调整大小
function resetSize(obj) {
    obj.height(obj.width() * 9 / 16);
    $(window).resize(function () {
        obj.height(obj.width() * 9 / 16);
    });
}